from datetime import datetime

from django.db import models


# Create your models here.

class Student(models.Model):
    """学生信息表"""
    name = models.CharField(max_length=50, verbose_name="姓名")
    age = models.SmallIntegerField(verbose_name="年龄")  # 小型的int类型
    sex = models.BooleanField(verbose_name="性别", default=False)

    class Meta:
        db_table = "sch_student"

    def __str__(self):
        return self.name

    @property  # 属性方法，自定义模型
    def achievement(self):
        """成绩列表"""
        # print(self.s_achievement.all())
        return self.s_achievement.values("student__name", "course__name")


class Course(models.Model):
    """课程表"""
    name = models.CharField(max_length=50, verbose_name="课程名称")
    teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, related_name="course", db_constraint=False)

    class Meta:
        db_table = "sch_course"

    def __str__(self):
        return self.name


class Teacher(models.Model):
    """老师表"""
    name = models.CharField(max_length=50, verbose_name="姓名")
    sex = models.BooleanField(verbose_name="性别", default=False)
    age = models.SmallIntegerField(verbose_name="年龄", default=33)  # 小型的int类型

    class Meta:
        db_table = "sch_teacher"

    def __str__(self):
        return self.name


class Achievement(models.Model):
    """成绩表"""
    score = models.DecimalField(default=0, max_digits=4, decimal_places=1, verbose_name="成绩")
    student = models.ForeignKey(Student, on_delete=models.DO_NOTHING, related_name="s_achievement", db_constraint=False)
    course = models.ForeignKey(Course, on_delete=models.DO_NOTHING, related_name="c_achievement", db_constraint=False)
    create_dtime = models.DateTimeField(auto_created=datetime.now)

    class Meta:
        db_table = "sch_achievement"

    def __str__(self):
        return str(self.score)
